package core.proto;

import static app.constants.CollabVizConstants.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.swing.text.Highlighter.Highlight;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Highlighter;

import org.apache.commons.io.FilenameUtils;

import java.util.*;
import core.util.logging.UserLogWriter;
import core.util.logging.constants.Action;
import core.util.logging.constants.Location;

/**
 * Contains code for highlighting with mouse. 
 */
public class PaintDoc extends JEditorPane
{
	private static final long serialVersionUID = 1L;

	Highlighter.HighlightPainter searchHighlightPainter;
	public final Highlighter.HighlightPainter userHighlightPainter;

	static boolean copyHighlightTextToNotepad = true;

	static String lastCopiedCase; // the last case whose hilited  text was copied to notepad. 
	
	List<Object> searchHilites;
	public final String docKey;
	boolean dragged = false;
	
	public PaintDoc(String docKey) {
		searchHighlightPainter = new TextHighlighter(SEARCH_NOTE_COLOR);
		userHighlightPainter = new TextHighlighter(OPEN_FILE_NODE_FOCUS_COLOR);
		searchHilites = new LinkedList<Object>();
		this.docKey = docKey;
		
		// Add existing highlights back to the doc. 
		if ( HighlightManager.get(docKey) != null){
			setUserHilites(HighlightManager.get(docKey));
		}
	}
	
	public static void setCopyHighlightTextToNotepad () {
		copyHighlightTextToNotepad = ! copyHighlightTextToNotepad;
	}

	public static boolean isCopyingHighlightTextToNotepad(){
		return copyHighlightTextToNotepad;
	}
	
	void setUserHilites(final HiliteData[] hilites){
		for (HiliteData hilite : hilites){
			try {
				getHighlighter().addHighlight(hilite.start, hilite.end, userHighlightPainter);
			} catch (BadLocationException e) {
			}
		}
	}
	

	public void addSearchHighlight(int start, int end){
		try {
			searchHilites.add(this.getHighlighter().addHighlight(start, end, searchHighlightPainter));
		} catch (BadLocationException e) {
			throw new Error(e);
		}
	}

	public void clearSearchHighlights(){
		for (Object tag : searchHilites){
			this.getHighlighter().removeHighlight(tag);
		}
	}
}
